package com.xssweibo.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.SparseArray;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;
import com.xssweibo.ui.Home;
import com.xssweibo.ui.IWeiboActivity;

public class BaseService extends Service implements Runnable{
	
	boolean bRun = true;
    private static final String CONSUMER_KEY = "1577372370";// 替换为开发者的appkey，例如"1646212860";
    private static final String REDIRECT_URL = "http://www.sina.com";
    
	public static List<Activity> activities = new ArrayList<Activity>();
	public static List<Task> tasks = new ArrayList<Task>();
	public static SparseArray<BitmapDrawable> arrayUserIcon = new SparseArray<BitmapDrawable>();
	public static HashMap<String, Oauth2AccessToken> sUser = new HashMap<String, Oauth2AccessToken>();
	public static String gCurrentUser;
	
	public static Weibo mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
	
	public static void saveUser(String uid, Oauth2AccessToken token){
		sUser.put(uid, token);
	}
	
	public static void addTask(Task t){
		tasks.add(t);
	}
	
	public static void addActivity(Activity activity){
		activities.add(activity);	
	}
	
	public static Activity getActivityByName(String name) {

		for (Activity ac : activities) {
			if (ac.getClass().getName().indexOf(name) >= 0) {
				return ac;
			}
		}

		return null;
	}

	@Override
	public void onCreate(){
		super.onCreate();
		Thread thread = new Thread(this);
		thread.start();
		
	}
	
	@Override
	public void run() {
		while (bRun) {
			if (!tasks.isEmpty()) {
				Task t = tasks.get(0);
				doTask(t);
				tasks.remove(t);
			}

			try {
				Thread.sleep(100);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	
	void doTask(Task t){
		Integer taskId = t.getTaskId();
		switch (taskId) {
		case Task.TASK_LOGIN:
			// 登陆完成
			gCurrentUser = (String)t.getParams().get("uid");
			break;
			
		case Task.TASK_GET_TIMELINE:
			StatusesAPI statusesAPI = new StatusesAPI(sUser.get(gCurrentUser));
			statusesAPI.friendsTimeline(0, 0, 50, 1, false, WeiboAPI.FEATURE.ALL, false, new TimelineListener());	
			break;			

		default:
			break;
		}
	}
	
	private static Handler handler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Task.TASK_GET_TIMELINE:
				// 解析json数据，刷新界面
				IWeiboActivity iWeiboActivity = (IWeiboActivity) getActivityByName("Home");
				iWeiboActivity.refresh(Home.REFRESH_WEIBO, msg.obj);
				break;

			default:
				break;
			}
		};
	};
	
	@Override
	public void onDestroy(){
		
	}
	
	@Override
	public void onLowMemory(){
		
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	class TimelineListener implements RequestListener{

		@Override
		public void onComplete(String arg0) {
			Message message = handler.obtainMessage();
			message.what = Task.TASK_GET_TIMELINE;
			message.obj = arg0;
			handler.sendMessage(message);
		}

		@Override
		public void onError(WeiboException arg0) {
			
		}

		@Override
		public void onIOException(IOException arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
